<?php
/**
 * 个人中心
 */
namespace app\api\modules\v1\owner\controllers;

use yii\web\Controller;
use app\api\components\ApiController;
use Yii;
use app\api\modules\v1\market\models\AppUser;
use app\models\base\User;
use app\models\base\BankAccount;
use yii\db\Query;
use app\api\modules\v1\market\models\Message;
use app\models\base\Activity;
use app\models\base\Project;
use app\api\modules\v1\market\models\UrlHandler;
use app\api\modules\v1\bank\models\BankApp;
use app\api\modules\v1\owner\models\Owner;

class DefaultController extends ApiController
{
	/**
	 * [actionIndex 个人信息]
	 * @return [type] [description]
	 */
    public function actionIndex()
    {
    	$get = Yii::$app->request->get();
       	//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($get['user_id'],$get['token']) == false) return parent::sendMessageJson('用户认证不通过');
        //查找个人信息
        $model = User::findOne(['user_id'=>$get['user_id'],'disabled'=>0]);
        if(!$model){
        	return parent::sendMessageJson('用户已离职,无法查看个人信息',parent::$CODE_ERR);
        }
        $info = (new Query())->select(['user_id','name'])->from(User::tableName().' AS user')->where(['user.disabled'=>0,'user.user_id'=>$get['user_id']])->one();
        if($get['status'] == 0){
            $user = (new Query())->select(['all_cost','all_value','all_gold'])
            						->from(BankAccount::tableName().' AS ba')
            						->where(['ba.disabled'=>0,'ba.user_id'=>$get['user_id']])
            						->one();
            $user = $this->formatuser($user);
        }else{
            $user = Owner::getvalues($get['user_id'],$get['status']);
        }
        // var_dump($user);exit;
        return parent::sendMessageJson(Message::SUCCESS,parent::$CODE_SUC,['info'=>$info,'user'=>$user]);

    }

    /**
     * [actionList 个人中心我处理的活动]
     * @return [type] [description]
     */
    public function actionList()
    {
    	$get = Yii::$app->request->get();
    	$page = Yii::$app->request->get('page',1);
    	//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($get['user_id'],$get['token']) == false) return parent::sendMessageJson('用户认证不通过');

        //查看个人活动列表
        $query = (new Query())->select(['activity_id','type','project_id','name','surplus_value','surplus_gold',
        	'all_cost','status','apply_user_id','handler_user_id','create_time','priority'])->from(Activity::tableName().' AS act')
        								->where(['act.handler_user_id'=>$get['user_id'],'act.disabled'=>0])
        								->orderBy(['last_modify'=>SORT_DESC]);
        $query = UrlHandler::handle_get($query,$get,['page','create_time','user_id','token']);
        $query = BankApp::getQueryByTime($get,$query);
        $data = BankApp::pagination($query,30,$page);
        // var_dump($data);exit;
        //格式化数据
    	if($data['list']){
    		$data = BankApp::Formatdata($data);
    	}

    	return ['code'=>self::$CODE_SUC,'message'=>'获取数据成功','data'=>$data['list'],'page'=>$data['pagesize']];

    }

    /**
     * [actionList 个人中心我创建的活动]
     * @return [type] [description]
     */
    public function actionCreatelist()
    {
    	$get = Yii::$app->request->get();
    	$page = Yii::$app->request->get('page',1);
    	//验证token
        $verifyToken = new AppUser();
        if($verifyToken->verifyAuth($get['user_id'],$get['token']) == false) return parent::sendMessageJson('用户认证不通过');

        //查看个人活动列表
        $query = (new Query())->select(['activity_id','type','project_id','name','surplus_value','surplus_gold',
        	'all_cost','status','apply_user_id','handler_user_id','create_time','priority'])->from(Activity::tableName().' AS act')
        								->where(['act.apply_user_id'=>$get['user_id'],'act.disabled'=>0])
        								->orderBy(['last_modify'=>SORT_DESC]);
        $query = UrlHandler::handle_get($query,$get,['page','create_time','user_id','token']);
        $query = BankApp::getQueryByTime($get,$query);
        $data = BankApp::pagination($query,30,$page);

        //格式化数据
    	if($data['list']){
    		$data = BankApp::Formatdata($data);
    	}

    	return ['code'=>self::$CODE_SUC,'message'=>'获取数据成功','data'=>$data['list'],'page'=>$data['pagesize']];

    }

    /**
     * 总价值等除8格式化
     * @param  [type] $user [description]
     * @return [type]       [description]
     */
    public function formatuser($user=[])
    {
        $user['all_value'] = round($user['all_value']/8,1);
        $user['all_gold'] = round($user['all_gold']/8,1);
        $user['all_cost'] = round($user['all_cost']/8,1);
        // var_dump($user);exit;
        return $user;
    }
}
